package com.wzl.producer.config.kafkaConfig;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * kafka自定义分区策略
 *
 * @author weizhongli
 * @since 2022-09-14 14:31
 */
@Component
public class CustomizePartitioner implements Partitioner {

    /**
     * 自定义分区策略
     */
    @Override
    public int partition(String s, Object o, byte[] bytes, Object o1, byte[] bytes1, Cluster cluster) {
        // 获取topic的分区列表
//        List<PartitionInfo> partitionInfoList = cluster.availablePartitionsForTopic(KafkaConfig.TOPIC);
//        int partitionCount = partitionInfoList.size();
//        int auditPartition = partitionCount - 1;
//        return auditPartition;
        // 自定义分区规则(这里假设全部发到0号分区)
        return 0;
    }

    /**
     * 在分区程序关闭时调用
     */
    @Override
    public void close() {

    }


    /**
     * 做必要的初始化工作
     */
    @Override
    public void configure(Map<String, ?> map) {

    }
}